﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Light_Notes
{
    /// <summary>
    /// Interaction logic for FindAndReplaceBar.xaml
    /// </summary>
    public partial class FindAndReplaceBar : UserControl
    {
        private Light_Notes.MainWindow mainWindow;
        private FindOptions findOptions;

        private NotesTab currentNotesTab
        {
            get { return mainWindow.Tabs.SelectedItem as NotesTab; }
        }

        public FindAndReplaceBar()
        {
            InitializeComponent();

            mainWindow = App.Current.MainWindow as Light_Notes.MainWindow;
            findOptions = FindOptions.None;
        }

        private void FindText_TextChanged(object sender, TextChangedEventArgs e)
        {
            currentNotesTab.FindIncremental(FindText.Text, findOptions);
        }

        private void FindText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0)
                    currentNotesTab.FindPrevious(FindText.Text, findOptions);
                else
                    currentNotesTab.FindNext(FindText.Text, findOptions);
            }
        }

        private void FindText_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                ReplaceText.Focus();
                e.Handled = true;
            }
        }

        private void MatchCase_Checked(object sender, RoutedEventArgs e)
        {
            findOptions |= FindOptions.MatchCase;
        }

        private void MatchCase_Unchecked(object sender, RoutedEventArgs e)
        {
            findOptions &= ~FindOptions.MatchCase;
        }

        private void MatchWholeWord_Checked(object sender, RoutedEventArgs e)
        {
            findOptions |= FindOptions.MatchWholeWord;
        }

        private void MatchWholeWord_Unchecked(object sender, RoutedEventArgs e)
        {
            findOptions &= ~FindOptions.MatchWholeWord;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            mainWindow.CloseFind();
        }

        private void FindPrevious_Click(object sender, RoutedEventArgs e)
        {
            currentNotesTab.FindPrevious(FindText.Text, findOptions);
        }

        private void FindNext_Click(object sender, RoutedEventArgs e)
        {
            currentNotesTab.FindNext(FindText.Text, findOptions);
        }

        private void ReplaceText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                currentNotesTab.Replace(FindText.Text, ReplaceText.Text, findOptions);
        }

        private void ReplaceText_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Tab)
            {
                FindText.Focus();
                e.Handled = true;
            }
        }

        private void Replace_Click(object sender, RoutedEventArgs e)
        {
            currentNotesTab.Replace(FindText.Text, ReplaceText.Text, findOptions);
        }

        private void ReplaceAll_Click(object sender, RoutedEventArgs e)
        {
            currentNotesTab.ReplaceAll(FindText.Text, ReplaceText.Text, findOptions);
        }
    }
}
